import { IFetchError } from './type';
import { AxiosRequestConfig, AxiosResponse, AxiosError } from 'axios';

export const requestOnFulfilled = (config: AxiosRequestConfig): AxiosRequestConfig => {
    if (config.method === 'get') config.params = config.data;
    return config;
};

export const requestOnRejected = (error: AxiosError): Promise<unknown> => {
    const err: IFetchError = { type: 'request', error };
    return Promise.reject(err);
};

export const responseOnFulfilled = (response: AxiosResponse): Promise<AxiosResponse> => {
    return Promise.resolve(response);
};

export const responseOnRejected = (error: AxiosError): Promise<unknown> => {
    const err: IFetchError = { type: 'response', error };
    return Promise.reject(err);
};
